﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;
using HZ.Supervise.Model.View;
using System.Data;
using HZ.Supervise;

public partial class Hospital_AreaStatistics : BasePage
{
    public Hospital_AreaStatistics()
    {
        //this.ModuleNo = Module.医院统计分析.ToString();
        //this.PageOperateType = OperateType.地区汇总统计.ToString();
    }

    IBLL_User iu = BLLFactory.BLL_User();
    KeyOperationLogJG kj;
    IBLL_Company ic = HZ.Supervise.BLLFactory.IBLL_Company();
    HZ.Supervise.IBLL.IExecuteContext i = HZ.Supervise.BLLFactory.ExecuteContext();
    SuperviseQuery vs = new SuperviseQuery();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //if (CurrentAccount.GetUser().Org.Area.AreaID != Constants.CURRENT_DEPLOY_AREAID)
            //{
            //    ddlCity.SelectedValue = CurrentAccount.GetUser().Org.Area.AreaID;
            //    ddlCity.Enabled = false;
            //}
            //ddlArea.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;
            //BindData(); 
        }
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AspNetPager_CompanyPS_PageChanged(object sender, EventArgs e)
    {
        BindData(); 
    }

    /// <summary>
    /// 绑定数据
    /// modify  by张雄  2011.5.4
    /// </summary>
    public void BindData()
    {
        string orderby = this.OrderFileds.SelectedValue + "  " + this.OrderType.SelectedValue;//排序字段与方式
        vs = Queryby();
        int t = 0;

        System.Data.DataTable dt = i.GetStatistics(4, orderby, vs, this.AspNetPager_CompanyPS.CurrentPageIndex, AspNetPager_CompanyPS.PageSize, out t);
        AspNetPager_CompanyPS.RecordCount = t;

        this.gvwPSLInfo.PageSize = AspNetPager_CompanyPS.PageSize;
        this.gvwPSLInfo.DataSource = dt;
        this.gvwPSLInfo.DataBind();

        dt = i.GetStatistics(4, orderby, vs, 1, 0, out t);
        double PurchaseAmountTotal = 0;
        double WareHouseAmountTotal = 0;
        double DistributeAmountTotal = 0;
        for (int j = 0; j < dt.Rows.Count; j++)
        {
            PurchaseAmountTotal += Convert.ToDouble(dt.Rows[j]["PurchaseAmount"]);
            WareHouseAmountTotal += Convert.ToDouble(dt.Rows[j]["WarehouseAmount"]);
            DistributeAmountTotal += Convert.ToDouble(dt.Rows[j]["DistributeAmount"]);
        }

        lblMemo.Text = "采购金额是" + PurchaseAmountTotal.ToString("0.00") + "元 配送金额是" + DistributeAmountTotal.ToString("0.00") + "元 入库金额是" + WareHouseAmountTotal.ToString("0.00")
            + "元";
        AddLog(string.Format("查询地市汇总统计数据!"));
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        AspNetPager_CompanyPS.CurrentPageIndex = 1;
    }

    protected void gvwInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Control cmdControl = e.CommandSource as Control;
        GridViewRow row = cmdControl.NamingContainer as GridViewRow;
        string str = row.Cells[1].Text;
        if (str != "总计")
        {
            vs = Queryby();
            vs.BackPage = "area";
            string sessionId = Guid.NewGuid().ToString();
            vs.AreaID = e.CommandArgument.ToString().Split('|')[0];
            vs.AreaName = e.CommandArgument.ToString().Split('|')[1];
            Session[sessionId] = vs;

            if (e.CommandName == "lbnP")
            {
                Response.Redirect("../Enterprise/PurchaseAmountDetail.aspx?sesid=" + sessionId);

            }

            if (e.CommandName == "lbnD")
            {
                Response.Redirect("../Enterprise/DistributeAmountDetail.aspx?sesid=" + sessionId);
            }
            if (e.CommandName == "lbnW")
            {
                Response.Redirect("../Enterprise/WarehouseAmountDetail.aspx?sesid=" + sessionId);
            }
        }
    }


    /// <summary>
    /// 分页    县区
    /// by张雄
    /// </summary>
    /// <param name="sender">事件发送源</param>
    /// <param name="e">事件参数</param>
    protected void CompanyPSL_PS_PageChanged(object sender, EventArgs e)
    {
        BindData();

    }


    /// <summary>
    /// 导出
    /// modify  by  张雄    2011.5.5
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        string orderby = this.OrderFileds.SelectedValue + "  " + this.OrderType.SelectedValue;//排序字段与方式
        vs = Queryby();
        int t = 0;
        System.Data.DataTable dt = i.GetStatistics(4, orderby, vs, 1, 0, out t);
        AddLog(string.Format("导出地市汇总统计数据成功!"));
        ExportFile.Instance.ExportData(dt, gvwPSLInfo, this.Page);
    }



    /// <summary>
    /// 查询条件
    /// </summary>
    /// <returns></returns>
    private SuperviseQuery Queryby()
    {
        //时间
        vs.Append += string.Format(" 1=1 ");
        DateTime startime;
        DateTime endtime;
        if (!string.IsNullOrEmpty(txtStratTime.Text.Trim()))
        {
            startime = (DateTime)new HZ.Common.ConvertHelper(txtStratTime.Text.Trim()).ToDateTime;//开始时间
            vs.Append += string.Format(" and AmountTime>= dbo.fun_GetYearMonth('{0}')", startime);
        }
        if (!string.IsNullOrEmpty(txtEndTime.Text.Trim()))
        {
            endtime = (DateTime)new HZ.Common.ConvertHelper(txtEndTime.Text.Trim()).ToDateTime;//结束时间
            vs.Append += string.Format(" and AmountTime <= dbo.fun_GetYearMonth('{0}') ", endtime);
        }
        return vs;
    }

    protected void HzButton1_Click(object sender, EventArgs e)
    {
        string sesid = Guid.NewGuid().ToString();
        ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>window.open('../Secondphase/Print/PrintPaymentVoucher.aspx?sesid=" + sesid + "');</script>");
    }
    /// <summary>
    /// 添加日志
    /// </summary>
    /// <param name="msg"></param>
    private void AddLog(string msg)
    {
        kj = new KeyOperationLogJG();
        kj.OperationLogSerialID = Guid.NewGuid().ToString();
        kj.Operator = CurrentAccount.GetUser().UserID;
        kj.OrgID = CurrentAccount.GetUser().Org.OrgID;
        kj.OperateTime = DateTime.Now;
        kj.OperateMessage = msg;
        iu.AddKeyOperationLog(kj);
    }

}
